package com.cellulam.framework.scaffold.generate;


import com.cellulam.framework.scaffold.generate.utils.GenUtils;
import com.cellulam.framework.scaffold.generate.db.JdbcConfig;
import com.cellulam.framework.scaffold.generate.project.MavenModuleInfo;
import com.cellulam.framework.scaffold.generate.project.MavenProjectInfo;

import java.io.IOException;

public class TemplateGeneratorFactory {

    private static final String jdbcResourcePath = "dev_jdbc.properties";

    private String mainPackage;
    private String mainGroupId;
    private String rootArtifactId;
    private String currentArtifactId;

    public TemplateGeneratorFactory(String mainPackage, String mainGroupId, String rootArtifactId, String currentArtifactId) {
        this.mainPackage = mainPackage;
        this.mainGroupId = mainGroupId;
        this.rootArtifactId = rootArtifactId;
        this.currentArtifactId = currentArtifactId;
    }

    public TemplateGenerator createGenerator() throws IOException {
        JdbcConfig config = JdbcConfig.parse(getClass(), jdbcResourcePath);

        String rootDirectory = GenUtils.getTopDir(getClass(), currentArtifactId, jdbcResourcePath);
        System.out.println("rootDirectory is " + rootDirectory);

        MavenProjectInfo projectInfo = new MavenProjectInfo(rootDirectory, mainPackage, mainGroupId, rootArtifactId);

        TemplateGenerator generator = new TemplateGenerator(config, projectInfo, config.getTableNames());
        generator.setServiceModule(new MavenModuleInfo("service", "service.services", projectInfo));
        generator.setDaoModule(new MavenModuleInfo("dao", "dao", projectInfo));
        generator.setDtoModule(new MavenModuleInfo("model",  "", projectInfo));
        generator.setSupportModule(new MavenModuleInfo("support", "support", projectInfo));
        return generator;
    }

}
